package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.SaleOrderScanConverter;
import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.exception.BusinessException;
import com.quad.innovators.salesease.mapper.warehouse.SaleOrderScanMapper;
import com.quad.innovators.salesease.model.entity.warehouse.ReturnOrderScan;
import com.quad.innovators.salesease.model.entity.warehouse.SaleOrderScan;
import com.quad.innovators.salesease.model.form.warehouse.SaleOrderScanForm;
import com.quad.innovators.salesease.model.vo.warehouse.SaleReturnScanNumberVO;
import com.quad.innovators.salesease.model.vo.warehouse.ScanCodeVO;
import com.quad.innovators.salesease.service.warehouse.SaleOrderDetailService;
import com.quad.innovators.salesease.service.warehouse.SaleOrderScanService;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/26 10:29
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class SaleOrderScanServiceImpl extends ServiceImpl<SaleOrderScanMapper, SaleOrderScan> implements SaleOrderScanService {

    private final SaleOrderScanConverter saleOrderScanConverter;

    @Override
    public List<ScanCodeVO> getScanDetail(String saleOrderDetailCode) {
        return this.baseMapper.getScanDetail(saleOrderDetailCode);
    }

    @Override
    public List<ScanCodeVO> getScanDetailBySaleReturnCode(String code) {
        return this.baseMapper.getScanDetailBySaleReturnCode(code);
    }

    @Override
    public String save(SaleOrderScanForm form) {
        SaleOrderScan entity = saleOrderScanConverter.toEntity(form);
        entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.SALE_ORDER_SCAN));
        boolean save = this.save(entity);
        return save ? entity.getCode() : "";
    }

    @Override
    public boolean delete(String code) {
        SaleOrderScan saleOrderScan = this.getOne(new QueryWrapper<SaleOrderScan>().lambda().eq(SaleOrderScan::getCode, code));
        if (ObjectUtils.isEmpty(saleOrderScan)) {
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return this.removeById(saleOrderScan.getId());
    }

    @Override
    public List<SaleOrderScanForm> getListByVehicleCode(String saleOrderDetailCode) {
        List<SaleOrderScan> saleOrderScanList = this.list(new QueryWrapper<SaleOrderScan>().lambda().eq(SaleOrderScan::getSaleOrderDetailCode, saleOrderDetailCode));
        return saleOrderScanList.stream().map(saleOrderScanConverter::toForm).toList();
    }

    @Override
    public List<SaleReturnScanNumberVO> getScanNumberInDetailCodeList(List<String> stringList) {
        return this.baseMapper.getScanNumberInDetailCodeList(stringList);
    }
}
